package MyApp
{
	import MyApp.Controller.StartupCommand;
	import MyApp.Model.proxy.LoginProxy;
	
	import components.admodel.ADMode1;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.core.FlexGlobals;
	import mx.core.IFlexDisplayObject;
	import mx.core.UIComponent;
	import mx.effects.Blur;
	import mx.effects.Iris;
	import mx.effects.Zoom;
	import mx.events.CloseEvent;
	import mx.events.EffectEvent;
	import mx.managers.PopUpManager;
	
	import net.lanelife.framework.cw.Window;
	import net.lanelife.framework.cw.WindowManager;
	import net.lanelife.framework.cw.events.WindowEvent;
	
	import org.puremvc.as3.interfaces.IFacade;
	import org.puremvc.as3.patterns.facade.Facade;
	
	import spark.components.TitleWindow;
	import spark.effects.Fade;
	
	import util.GDUtil;

	public class MyAppFacade extends Facade implements IFacade
	{
		public static const APP_STARTUP:String = "app_startup";
		public static const APP_STARTUP_OVER:String = "app_startup_over";
		
		[Bindable]
		public static var temp:IFlexDisplayObject;
		public function MyAppFacade()
		{
			super();
			alertLableChiness();
		}
		
		public static function getInstance():MyAppFacade{
			if(instance==null) instance = new MyAppFacade();
			
			return instance as MyAppFacade;
		}	
		
		private function alertLableChiness():void {
			Alert.yesLabel = "是";
			Alert.noLabel = "否";
			Alert.cancelLabel = "取消";
			Alert.okLabel = "确定";
		}
		
		//初始化command
		override protected function initializeController():void{
			super.initializeController();
			//register some Commands
			registerCommand(APP_STARTUP,StartupCommand);
		}
		
		//初始化proxy
		override protected function initializeModel():void{
			super.initializeModel();
		}
		public function startup(app:Object):void{	
			sendNotification(APP_STARTUP,app);	
		}	
		public static function openVirtualWindow(windowClass:Class,ownedWindows:Window=null,MediatorClass:Class=null, name:String=null):Window{
			var baseWindow:Window = WindowManager.get(windowClass.NAME) as Window;
			if (!baseWindow) 
			{
				baseWindow = new windowClass();
				baseWindow.id = windowClass.NAME;
			}
			if(MediatorClass!=null){
				getInstance().registerMediator(new MediatorClass(name,baseWindow));
			}
			baseWindow.addEventListener(WindowEvent.WINDOW_CLOSED,function (e:WindowEvent):void{
				getInstance().removeMediator(MediatorClass.NAME);
			});
			baseWindow.ownerWindow=ownedWindows;
			baseWindow.modal=true;
			baseWindow.show();
			baseWindow.x=FlexGlobals.topLevelApplication.width/2-baseWindow.width/2;//水平居中
			baseWindow.y=FlexGlobals.topLevelApplication.height/2-baseWindow.height/2;//垂直居中
			return baseWindow
		}
		//提供弹出框的管理
		public static function openPopUpWindow( ComponentClass:Class, MediatorClass:Class=null, name:String=null ):Object
		{
			/*var mShowEffect:Blur=new Blur();
			mShowEffect.blurXFrom=255;
			mShowEffect.blurYFrom=255;
			mShowEffect.blurXTo=0;
			mShowEffect.blurYTo=0;*/	
/*			mShowEffect.target=window;
			mShowEffect.duration=800;*/
			//mShowEffect.play();
			var window:IFlexDisplayObject = PopUpManager.createPopUp( FlexGlobals.topLevelApplication as Sprite, ComponentClass, true );
			if(MediatorClass !=null) { 
				MyAppFacade.getInstance().registerMediator( new MediatorClass( name,window ) );
			}
			PopUpManager.centerPopUp( window );
			return window; 
		}
		public static function closePopUpWindow( window:IFlexDisplayObject, mediatorName:String ):void
		{
			PopUpManager.removePopUp( window );
			MyAppFacade.getInstance().removeMediator( mediatorName ); 
			MyAppFacade.temp=null;//清空弹出窗口的记录
		}
		
		
		public static function openLonelyPopUpWindow( ComponentClass:Class):Object{
			var mShowEffect:Blur=new Blur();
			mShowEffect.blurXFrom=255;
			mShowEffect.blurYFrom=255;
			mShowEffect.blurXTo=0;
			mShowEffect.blurYTo=0;	
			var window:IFlexDisplayObject = PopUpManager.createPopUp( FlexGlobals.topLevelApplication as Sprite, ComponentClass, true );
			mShowEffect.target=window;
			mShowEffect.duration=800;
			PopUpManager.centerPopUp( window );
			return window;
		}
		public static function closeLonelyPopUpWindow(window:IFlexDisplayObject):void{
			PopUpManager.removePopUp( window );
		}
		
		
		/**
		 * 弹出广告框title
		 */ 
		
		public static function openAdTitle(ComponentClass:Class=null,MediatorClass:Class=null, name:String=null):void {
			var point1:Point = new Point();
			if(ComponentClass == null) { 
				var ad1:ADMode1 = ADMode1(PopUpManager.createPopUp( FlexGlobals.topLevelApplication as Sprite, ADMode1, false));
				var sp:Sprite = FlexGlobals.topLevelApplication as Sprite;
				ad1.x = (point1.x + sp.width - ad1.width);
				ad1.y = (point1.y + sp.height - ad1.height);
				
				var showFade:Fade=new Fade();
				showFade.alphaFrom=0;
				showFade.alphaTo=1;
				showFade.duration=3000;
				showFade.target = ad1;
				showFade.play();
				//记录当前窗口
				MyAppFacade.temp=ad1;
			}else {
				
				var window:IFlexDisplayObject = PopUpManager.createPopUp( FlexGlobals.topLevelApplication as Sprite, ComponentClass, false );
				if(MediatorClass !=null) { 
					MyAppFacade.getInstance().registerMediator( new MediatorClass(name,window));
				}
				var sp:Sprite = FlexGlobals.topLevelApplication as Sprite;

				window.x = (point1.x + sp.width - window.width);
				window.y = (point1.y + sp.height - window.height);
				
				var showFade:Fade=new Fade();
				showFade.alphaFrom=0;
				showFade.alphaTo=1;
				showFade.duration=3000;
				showFade.target = window;
				showFade.play();
				//记录当前窗口
				MyAppFacade.temp=window;
			}
		}
		public static function registerDynamicMediator(mediatorName:String, mediatorClass:Class, component:Object):void{
			MyAppFacade.getInstance().registerMediator( new mediatorClass( mediatorName,component ) );
		}
	}
}